﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;
using AnimaGestionPersonnage.Properties;

namespace AnimaGestionPersonnage.Vue
{
    public partial class FormCreateCharClass : Form
    {
        private Boolean saved = true;
        private CharacterClass classe = new CharacterClass();
        private FormSkillModder skillModder = new FormSkillModder();
        private int state = Utils.DEFAULT_STATE;
        private string filename;

        public FormCreateCharClass()
        {
            InitializeComponent();
        }

        #region EVENT_BUTTON

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (saved)
            {
                MessageBox.Show(this, "Données déjà suavegardé.", "Sauvegarde", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                saveFileDialogCharacterClass.FileName = Settings.Default.FilePath + Settings.Default.ClassList;

                DialogResult result = DialogResult.OK;

                if (filename == null)
                {
                    result = saveFileDialogCharacterClass.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        filename = saveFileDialogCharacterClass.FileName;
                    }
                }

                if (result == DialogResult.OK)
                {
                    if (Utils.classList.Contains(classe))
                    {
                        Utils.classList.Remove(classe);
                    }
                    Utils.classList.Add(classe);

                    Utils.xmlSerializeClasses(filename);
                    saved = true;
                    buttonSave.Enabled = false;
                }
            }
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            DialogResult frmclose = DialogResult.Yes;
            if (!saved)
            {
                frmclose = MessageBox.Show("Voulez-vous ouvrir une nouvelle classe sans sauvegarder celle-ci?", "Attention",
                                                                MessageBoxButtons.YesNo,
                                                                MessageBoxIcon.Warning,
                                                                MessageBoxDefaultButton.Button2);
            }
            if (frmclose == DialogResult.Yes)
            {
                classe = new FormEditionSelection().ShowDialog(this);

                if (classe != null)
                {
                    SuspendLayout();
                    state = Utils.OPEN_STATE;

                    foreach (SkillMod nc in classe.normalCosts)
                    {
                        switch (nc.skillName)
                        {
                            case Utils.LP_MULTI:
                                textBoxMultiLP.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.ATTACK:
                                textBoxAttack.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.BLOCK:
                                textBoxBlock.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.DODGE:
                                textBoxDodge.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.WEAR_ARMOR:
                                textBoxWearArmor.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.KI:
                                textBoxKi.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.ACC_KI:
                                textBoxAccumulKi.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.ZEON:
                                textBoxZeon.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.MA_MULTIP:
                                textBoxMultiMA.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.MAGIC_PRO:
                                textBoxMagicPro.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.SUMMON:
                                textBoxSummon.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.CONTROL:
                                textBoxControl.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.BIND:
                                textBoxBind.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.BANISH:
                                textBoxBanish.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.PPP:
                                textBoxPPP.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.PSY_PRO:
                                textBoxPsyPro.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.MARTIAL_LIMIT:
                                textBoxMartialLimit.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.MYSTIC_LIMIT:
                                textBoxMysticLimit.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.PSY_LIMIT:
                                textBoxPsyLimit.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.ATHLETIC:
                                textBoxAthletics.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.VIGOR:
                                textBoxVigor.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.PERCEPTION:
                                textBoxPerceptive.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.INTELECTUAL:
                                textBoxIntellectual.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.SOCIAL:
                                textBoxSocial.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.SUBTERFUGE:
                                textBoxSubterfuge.Text = Convert.ToString(nc.value);
                                break;
                            case Utils.CREATIVE:
                                textBoxCreative.Text = Convert.ToString(nc.value);
                                break;
                        }
                    }
                        
                    textBoxClassName.Text = classe.name;
                    textBoxInatePP.Text = Convert.ToString(classe.pppLevel);
                    textBoxInitiative.Text = Convert.ToString(classe.initiative);
                    textBoxLifePoint.Text = Convert.ToString(classe.lpLevel);
                    textBoxMartialKnowledge.Text = Convert.ToString(classe.DILevel);
                    comboBoxArchetype.SelectedIndex = (int)classe.type;

                    listBoxReducedCost.Items.Clear();
                    foreach (SkillMod rdc in classe.reducedCosts)
                    {
                        addReducedCost(rdc.skillName, rdc.value);
                    }

                    listBoxInnateBonus.Items.Clear();
                    foreach (SkillMod lb in classe.levelBonuses)
                    {
                        addInnateBonus(lb.skillName, lb.value);
                    }

                    ResumeLayout();
                    state = Utils.DEFAULT_STATE;
                }
            }
        }

        private void buttonAddReducedCost_Click(object sender, EventArgs e)
        {
            skillModder.ShowDialog(this, Utils.REDUCED_COST);
        }

        private void buttonRemoveReducedCost_Click(object sender, EventArgs e)
        {
            if (listBoxReducedCost.SelectedItem != null)
            {
                classe.reducedCosts.RemoveAt(listBoxReducedCost.SelectedIndex);
                listBoxReducedCost.Items.RemoveAt(listBoxReducedCost.SelectedIndex);
            }
        }

        private void buttonAddInnateBonus_Click(object sender, EventArgs e)
        {
            skillModder.ShowDialog(this, Utils.INNATE_BONUS);
        }

        private void buttonRemoveInnateBonus_Click(object sender, EventArgs e)
        {
            if (listBoxInnateBonus.SelectedItem != null)
            {
                classe.levelBonuses.RemoveAt(listBoxInnateBonus.SelectedIndex);
                listBoxInnateBonus.Items.RemoveAt(listBoxInnateBonus.SelectedIndex);
            }
        }

        #endregion
        
        /// <summary>
        /// verifie si les donnees ont ete sauvegarde et si oui(ou qu'on force la fermeture) affiche la fenetre parent
        /// </summary>
        /// <param name="e">evenement de fermeture de la fenetre</param>
        private void FormCreateCharClass_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = Utils.onCloseEvent(saved, this, null,e);
        }

        /// <summary>
        /// active le bouton de sauvegarde et flag comme non sauvegarde
        /// </summary>
        private void dataChanged()
        {
            if (state == Utils.DEFAULT_STATE)
            {
                saved = false;
                buttonSave.Enabled = true;
            }
        }

        public void addReducedCost(String s,int i)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                classe.reducedCosts.Add(new SkillMod(s, i));
            }
            listBoxReducedCost.Items.Add("+1" + s + " : " + i);
            dataChanged();
        }

        public void addInnateBonus(String s, int i)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                classe.levelBonuses.Add(new SkillMod(s, i));
            }
            listBoxInnateBonus.Items.Add(s + " +" + i);
            dataChanged();
        }

        private void addNormalCost(string nameSkill, int cost)
        {
            if (cost > 0)
            {
                foreach (SkillMod nc in classe.normalCosts)
                {
                    if (nc.Equals(nameSkill))
                    {
                        classe.normalCosts.Remove(nc);
                        break;
                    }
                }
                classe.normalCosts.Add(new SkillMod(nameSkill, cost));
                dataChanged();
            }
        }

        #region EVENT_COMPONENTS

        private void textBoxClassName_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                classe.name = textBoxClassName.Text;
                dataChanged();
            }
        }

        private void comboBoxArchetype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                classe.type = (Archetype)comboBoxArchetype.SelectedIndex;
                dataChanged();
            }
        }

        private void textBoxMultiLP_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.LP_MULTI, Utils.getIntFromString(textBoxMultiLP.Text, this));
            }
        }

        private void textBoxLifePoint_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                int tempVal = Utils.getIntFromString(textBoxLifePoint.Text, this);

                if (tempVal > 0)
                {
                    classe.lpLevel = tempVal;
                    dataChanged();
                }
            }
        }

        private void textBoxInitiative_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                int tempVal = Utils.getIntFromString(textBoxInitiative.Text, this);

                if (tempVal > 0)
                {
                    classe.initiative = tempVal;
                    dataChanged();
                }
            }
        }

        private void textBoxMartialKnowledge_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                int tempVal = Utils.getIntFromString(textBoxMartialKnowledge.Text, this);

                if (tempVal > 0)
                {
                    classe.DILevel = tempVal;
                    dataChanged();
                }
            }
        }

        private void textBoxInatePP_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                float tempVal = Utils.getFloatFromString(textBoxInatePP.Text, this);

                if (tempVal > 0)
                {
                    classe.pppLevel = tempVal;
                    dataChanged();
                }
            }
        }

        private void textBoxMartialLimit_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.MARTIAL_LIMIT, Utils.getIntFromString(textBoxMartialLimit.Text, this));
            }
        }

        private void textBoxAttack_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.ATTACK, Utils.getIntFromString(textBoxAttack.Text, this));
            }
        }

        private void textBoxBlock_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.BLOCK, Utils.getIntFromString(textBoxBlock.Text, this));
            }
        }

        private void textBoxDodge_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.DODGE, Utils.getIntFromString(textBoxDodge.Text, this));
            }
        }

        private void textBoxWearArmor_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.WEAR_ARMOR, Utils.getIntFromString(textBoxWearArmor.Text, this));
            }
        }

        private void textBoxKi_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.KI, Utils.getIntFromString(textBoxKi.Text, this));
            }
        }

        private void textBoxMysticLimit_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.MYSTIC_LIMIT, Utils.getIntFromString(textBoxMysticLimit.Text, this));
            }
        }

        private void textBoxZeon_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.ZEON, Utils.getIntFromString(textBoxZeon.Text, this));
            }
        }

        private void textBoxMultiMA_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.MA_MULTIP, Utils.getIntFromString(textBoxMultiMA.Text, this));
            }
        }

        private void textBoxMagicPro_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.MAGIC_PRO, Utils.getIntFromString(textBoxMagicPro.Text, this));
            }
        }

        private void textBoxSummon_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.SUMMON, Utils.getIntFromString(textBoxSummon.Text, this));
            }
        }

        private void textBoxControl_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.CONTROL, Utils.getIntFromString(textBoxControl.Text, this));
            }
        }

        private void textBoxBind_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.BIND, Utils.getIntFromString(textBoxBind.Text, this));
            }
        }

        private void textBoxBanish_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.BANISH, Utils.getIntFromString(textBoxBanish.Text, this));
            }
        }

        private void textBoxPsyLimit_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.PSY_LIMIT, Utils.getIntFromString(textBoxPsyLimit.Text, this));
            }
        }

        private void textBoxPPP_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.PPP, Utils.getIntFromString(textBoxPPP.Text, this));
            }
        }

        private void textBoxPsyPro_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.PSY_PRO, Utils.getIntFromString(textBoxPsyPro.Text, this));
            }
        }

        private void textBoxAthletics_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.ATHLETIC, Utils.getIntFromString(textBoxAthletics.Text, this));
            }
        }

        private void textBoxSocial_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.SOCIAL,  Utils.getIntFromString(textBoxSocial.Text, this));
            }
        }

        private void textBoxPerceptive_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.PERCEPTION, Utils.getIntFromString(textBoxPerceptive.Text, this));
            }
        }

        private void textBoxIntellectual_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.INTELECTUAL, Utils.getIntFromString(textBoxIntellectual.Text, this));
            }
        }

        private void textBoxVigor_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.VIGOR, Utils.getIntFromString(textBoxVigor.Text, this));
            }
        }

        private void textBoxSubterfuge_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.SUBTERFUGE, Utils.getIntFromString(textBoxSubterfuge.Text, this));
            }
        }

        private void textBoxCreative_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.CREATIVE, Utils.getIntFromString(textBoxCreative.Text, this));
            }
        }
        
        private void textBoxAccumulKi_TextChanged(object sender, EventArgs e)
        {
            if (state == Utils.DEFAULT_STATE)
            {
                addNormalCost(Utils.ACC_KI, Utils.getIntFromString(textBoxAccumulKi.Text, this));
            }
        }

        #endregion
    }
}
